﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using DigitalSolutionLibrary.SqlDatabase;

namespace DigitalSolutionManagement
{
    public partial class frmChoiceProduct : BaseFormEdit
    {
        private Thread th;
        private delegate void UpdateGrid(DataSet ds);
        private DataSet dsProduct;
        private int choiceFor;

        public int ProId { get; set; }
        public string ProCode { get; set; }
        public string ProName { get; set; }
        public decimal ProPrice { get; set; }
        public string UnitName { get; set; }
        public int ActionType { get; set; }

        private void LoadProduct()
        {
            ParameterStock stock = new ParameterStock();
            Parameter par = new Parameter("type", choiceFor, SqlDbType.Int);
            stock.AddParamter(par);

            object obj = Program.dbWrapper.ExecuteStore("GetAllProductList", stock, DatabaseWrapper.ReturnType.DATASET);
            if (obj != null)
            {
                dsProduct = (DataSet)obj;
                UpdateGrid delg = new UpdateGrid(FillGridData);
                Invoke(delg, new object[] { dsProduct });
            }
            else
            {
                processing1.HideProcessing();
                MessageBox.Show("Không thể kết nối đến server, vui lòng thử lại sau", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void FillGridData(DataSet ds)
        {
            modifiedDataGridView1.DataSource = ds.Tables[0];
            processing1.HideProcessing();
        }

        /// <summary>
        /// Choice product
        /// </summary>
        /// <param name="type">0: Receiving; 1: Delivery</param>
        public frmChoiceProduct(int type)
        {
            InitializeComponent();
            choiceFor = type;
        }

        private void frmChoiceProduct_Load(object sender, EventArgs e)
        {
            processing1.ShowProcessing();
            th = new Thread(() => LoadProduct());
            th.Start();
        }

        private void frmChoiceProduct_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (th != null && th.IsAlive)
                th.Abort();
        }

        private void txtKeyWord_TextChanged(object sender, EventArgs e)
        {
            if (dsProduct != null)
                dsProduct.Tables[0].DefaultView.RowFilter = String.Format("ProName LIKE '%{0}%'", txtKeyWord.Text);
        }

        private void modifiedDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                //if ((int)modifiedDataGridView1.SelectedRows[0].Cells["ColQty"].Value == 0 && ActionType == 1)
                //{
                //    MessageBox.Show("Số lượng trong kho không còn đủ để xuất", "DSCo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                //}
                //else
                {
                    ProId = (int)modifiedDataGridView1.SelectedRows[0].Cells["ColId"].Value;
                    ProName = modifiedDataGridView1.SelectedRows[0].Cells["ColProName"].Value.ToString();
                    ProCode = modifiedDataGridView1.SelectedRows[0].Cells["ColProCode"].Value.ToString();
                    ProPrice = (decimal)modifiedDataGridView1.SelectedRows[0].Cells["ColPrice"].Value;
                    UnitName = modifiedDataGridView1.SelectedRows[0].Cells["ColUnit"].Value.ToString();

                    DialogResult = System.Windows.Forms.DialogResult.OK;
                }
            }
        }
    }
}
